﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Security.Cryptography;

public partial class Main_UserUpdate : ValidateBase
{
    UsersDao ud = new UsersDao();
    UsersVo uv = new UsersVo();
    static bool update;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            update = false;
            if (uv.Uid == null)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
                txtnickname.Text = uv.Unickname;
                txtemail.Text = uv.Uemail;
                ddlsex.SelectedValue = uv.Usex;
                txttruename.Text = uv.Utruename;
                txtidcard.Text = uv.Uidcard;
                txtphone.Text = uv.Uphone;
                txttel.Text = uv.Utel;
                txtaddress.Text = uv.Uaddress;
                txtqq.Text = uv.Uqq;
                txtmsn.Text = uv.Umsn;
                txtsign.Text = uv.Usign;
            }
        }
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        if (update || txtVail.Text == "")
        {
            string script = "alert('验证码不可为空！')";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        update = true;
        if (txtVail.Text == base.strValidate)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            uv = (UsersVo)Session["user"];
            bool flag = CheckSpecialWord(txttruename);
            if (flag)
            {
                string script = "alert('真实姓名填写不正确！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                return;
            }
            uv.Unickname = txtnickname.Text;
            uv.Uemail = txtemail.Text;
            uv.Usex = ddlsex.SelectedValue;
            uv.Utruename = txttruename.Text;
            uv.Uidcard = txtidcard.Text;
            uv.Uphone = txtphone.Text;
            uv.Utel = txttel.Text;
            uv.Uaddress = txtaddress.Text;
            uv.Uqq = txtqq.Text;
            uv.Umsn = txtmsn.Text;
            uv.Usign = txtsign.Text;
            Exception ex = new Exception();
            ex = ud.updateUsers(uv);
            if (ex == null)
            {
                string script = "alert('修改资料成功！');location.href='../index.aspx';";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            }
            else
            {
                string script = "alert('修改资料失败！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            }
        }
        else
        {
            string script = "alert('验证码错误！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
    }
    protected void btnupdatepwd_Click(object sender, EventArgs e)
    {
        if (txtoldpwd.Text==""||txtpwd.Text == ""||txtpwdagain.Text=="")
        {
            string script = "alert('密码不能为空！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
        else
        {
            uv = (UsersVo)Session["user"];
            txtoldpwd.Text = MD5(txtoldpwd.Text);
            if (txtoldpwd.Text != uv.Upwd)
            {
                string script = "alert('原密码错误！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            }
            else
            {
                Exception ex = new Exception();
                ex = ud.updateUsersPwd(uv.Uid.ToString(),txtpwd.Text);
                if (ex == null)
                {
                    string script = "alert('修改密码成功！');location.href='../index.aspx';";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                }
                else
                {
                    string script = "alert('修改密码失败！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                }
            }
        }
    }

    public static string MD5(string str)
    {
        byte[] b = Encoding.Default.GetBytes(str);
        b = new MD5CryptoServiceProvider().ComputeHash(b);
        string ret = "";
        for (int i = 0; i < b.Length; i++)
            ret += b[i].ToString("x").PadLeft(2, '0');
        return ret;
    }

    public bool CheckSpecialWord(TextBox txtbox)
    {
        bool flag = false;
        ArrayList list = new ArrayList();
        list.Add(" ");
        list.Add("~");
        list.Add("!");
        list.Add("@");
        list.Add("#");
        list.Add("$");
        list.Add("%");
        list.Add("^");
        list.Add("&");
        list.Add("*");
        list.Add("(");
        list.Add(")");
        list.Add("_");
        list.Add("+");
        list.Add("<");
        list.Add(">");
        list.Add(",");
        list.Add(".");
        list.Add("/");
        list.Add("\\");
        list.Add("?");
        list.Add(";");
        for (int i = 0; i < txtbox.Text.Length; i++)
        {
            if (list.Contains(txtbox.Text[i].ToString()))
            {
                flag = true;
            }
        }
        return flag;
    }
}
